package kin.proxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Locale;

import org.springframework.stereotype.Component;






import kin.context.system.RequestContext;
import kin.system.util.ApplicationContextHelper;
import kin.system.util.StringUtil;


public class I18nProxy implements InvocationHandler  
{   
	   /** 
	      Constructs a TraceHandler 
	      @param t the implicit parameter of the method call 
	   */  
	   public I18nProxy(Class t)  
	   {    
	      try {
			target = t.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	   }  
	  
	   public Object invoke(Object proxy, Method m, Object[] args) throws Throwable  
	   {                                                          
	     
	      if (args != null)  
	      {  
	         String label=(String) args[0];
	         if(null!=label&&!"".equals(label)&&m.getName()!="list"&&m.getName()!="boxselect"&&m.getName()!="actioncolumn"&&m.getName()!="boxselect"&&m.getName()!="button"&&m.getName()!="checkboxgroup"){
	        	 
	        	String language= null;
	        	language=(String)RequestContext.getRequest().getSession().getAttribute("language");
	        	if (StringUtil.isEmpty(language)){
	        		language=(String)RequestContext.getRequest().getParameter("language");
	        	}else{
	        		
	        	}
	        	Locale locale = null;
	        	switch(language){
	        	case "en":locale=Locale.US; break;
	        	case "zh":locale=Locale.CHINA; break;
	        	}
	        	
	        	 
	        	 args[0]= ApplicationContextHelper.getApplicationContext().getMessage(label, null, locale);
	        	 System.out.println("--------------"+args[0]);
	         }
	      }  
	     
	      return m.invoke(target, args);  
	   }  
	  
	   private Object target;  
	
}
